package toubiao.factory;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import spring.SpringContextHolder;
import toubiao.dao.BaseDaoI;
import toubiao.dao.business.ContactDaoI;
import toubiao.dao.business.ContractDaoI;
import toubiao.dao.business.ContractPhotoDaoI;
import toubiao.dao.business.DepartmentDaoI;
import toubiao.dao.business.DesignerDaoI;
import toubiao.dao.business.PartADaoI;
import toubiao.dao.business.ProjectDaoI;
import toubiao.dao.business.ProjectSnDaoI;
import toubiao.domain.po.business.PoContact;
import toubiao.domain.po.business.PoContract;
import toubiao.domain.po.business.PoContractPhoto;
import toubiao.domain.po.business.PoDepartment;
import toubiao.domain.po.business.PoDesigner;
import toubiao.domain.po.business.PoPartA;
import toubiao.domain.po.business.PoProject;
import toubiao.domain.po.business.PoProjectSn;

@Component
public class FactoryDao {

	@Autowired
	private DepartmentDaoI departmentDao;
	
	@Autowired
	private ProjectDaoI projectDao;
	
	@Autowired
	private DesignerDaoI designerDaoI;
	
	@Autowired
	private SpringContextHolder contextHolder;
	
	static private Map<Class,Class> classMap=new HashMap<Class, Class>();
	{
		classMap.put(PoDepartment.class, DepartmentDaoI.class);
		classMap.put(PoProject.class, ProjectDaoI.class);
		classMap.put(PoDesigner.class, DesignerDaoI.class);
		classMap.put(PoContact.class, ContactDaoI.class);
		classMap.put(PoContract.class, ContractDaoI.class);
		classMap.put(PoPartA.class, PartADaoI.class);
		classMap.put(PoProjectSn.class, ProjectSnDaoI.class);
		classMap.put(PoContractPhoto.class, ContractPhotoDaoI.class);
	}
	
	
	public <T> BaseDaoI<T> getDao(Class<T> clazz) {
		if(clazz==null){
			throw new RuntimeException("clazz is null");
		}
		String classSimpleName=clazz.getSimpleName();
		if(classSimpleName.equalsIgnoreCase("PoDepartment")){
			return (BaseDaoI<T>) departmentDao;
		}
		return null;
	}
	
	public <T> BaseDaoI<T> getDaoByPoClass(Class<T> clazz) {
		Class daoClass=classMap.get(clazz);
		if(daoClass==null){
			throw new RuntimeException("map 中未定义 Po为"+clazz.getSimpleName()+"对应的dao");
		}
		return (BaseDaoI<T>) contextHolder.getBean(daoClass);
	}
}
